package {
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	

	public class WordNetGetData extends Sprite
	{
		private var tf:TextField;
		private var resultTF:TextField
		private var tFormat:TextFormat
		
		public function WordNetGetData()
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			init();
		}
		
		private function init():void {
			tf = createTextField(0, 20, 100, 20);
			tf.background = true;
			tFormat = new TextFormat("Verdana");
			tFormat.font = "_sans";
			tf.defaultTextFormat = tFormat;
			tf.text = "hello";
			tf.type = TextFieldType.INPUT;
			tf.textColor = 0x000000;
			tf.embedFonts = false;
			
			//trace(tf.htmlText);
			resultTF = createTextField(0, 50, 100, 20);
			
			resultTF.autoSize = TextFieldAutoSize.LEFT;
			resultTF.defaultTextFormat = tFormat;
			var myButton:SimpleButton = new SimpleButton();
		 	var downSprite:Sprite = new Sprite();
			downSprite.graphics.lineStyle(2, 0x202020);
			downSprite.graphics.beginFill(0x00FF00);
			downSprite.graphics.drawRect(110, 20, 50, 20);
			 
			var upSprite:Sprite = new Sprite();
			upSprite.graphics.lineStyle(2, 0x202020);
			upSprite.graphics.beginFill(0xFFFF00);
			upSprite.graphics.drawRect(110, 20, 50, 20);
			
			myButton.upState = upSprite;
			myButton.overState = upSprite;
			myButton.downState = downSprite;
			myButton.useHandCursor = true;
			myButton.hitTestState = upSprite;
			 
			this.addChild(myButton);

			myButton.addEventListener(MouseEvent.CLICK, search);
		}
		
		private function createTextField(x:Number, y:Number, width:Number, height:Number):TextField {
            var result:TextField = new TextField();
            result.x = x; result.y = y;
            result.width = width; result.height = height;
            
            addChild(result);
            return result;
        }

		
		private function search(e:MouseEvent):void {
			var variable:URLVariables = new URLVariables();
			variable.word = tf.text;
			variable.submit = "Query";
			//var request:URLRequest = new URLRequest("http://www.ujinsung.com/temp/for_darren/xmlrpc_proxy.php?url=http://jws-champo.ac-toulouse.fr:8080/wordnet-xml/servlet&submit=Qurery&word="+tf.text);
			// for local test.
			var request:URLRequest = new URLRequest("http://jws-champo.ac-toulouse.fr:8080/wordnet-xml/servlet");
			var loader:URLLoader = new URLLoader();
			
			request.method = URLRequestMethod.POST;
			request.data = variable;
			loader.load(request);
			loader.addEventListener(Event.COMPLETE, onGetWordNetXML);
		}
		
		private function onGetWordNetXML(e:Event):void {
			var wnXML:XML = new XML(e.target.data);
			var partOfWord:XMLList = wnXML.pos.@name;
			
			
			var isNone:Boolean = false;
			for(var i:int=0; i<partOfWord.length(); i++){
				if(partOfWord[i] == "noun") {
					isNone = true
				}
			}
			
			
			if (isNone) {
				resultTF.text = "sing a song with this none " + tf.text;
			}else {
				resultTF.text = "dame, you cannot sing now with this word " + tf.text + "!!!";
			}	
		}
	}
}
